package com.spotbook.providers;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class SpotContentProvider extends ContentProvider {
	private static final int SPOTS = 0;
	private static final int SPOT_ID = 1;

	private static final UriMatcher MATCHER = new UriMatcher(
			UriMatcher.NO_MATCH);

	static {
		MATCHER.addURI(Contract.AUTHORITY, "spots", SPOTS);
		MATCHER.addURI(Contract.AUTHORITY, "spots/#", SPOT_ID);
	}

	private SpotHelper helper;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = this.helper.getWritableDatabase();
		int returned = 0;
		switch (MATCHER.match(uri)) {
		case SPOT_ID:
			returned = db.delete(Contract.TABLE,
					Contract._ID + "=" + Contract.getId(uri), null);
			break;
		case SPOTS:
			returned = db.delete(Contract.TABLE, selection, selectionArgs);
			break;
		default:
			throw new SQLException("failed to match uri: " + uri);
		}
		if (returned > 0) {
			getContext().getContentResolver().notifyChange(uri, null);
		}
		return returned;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = this.helper.getWritableDatabase();
		int returned = 0;
		switch (MATCHER.match(uri)) {
		case SPOT_ID:
			returned = db.update(Contract.TABLE, values, Contract._ID + "="
					+ Contract.getId(uri), null);
			break;
		case SPOTS:
			returned = db.update(Contract.TABLE, values, selection,
					selectionArgs);
			break;
		default:
			throw new SQLException("failed to match uri: " + uri);
		}
		if (returned > 0) {
			getContext().getContentResolver().notifyChange(uri, null);
		}
		return returned;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Uri created = uri;
		SQLiteDatabase db = this.helper.getWritableDatabase();
		Long id = values.getAsLong(Contract._ID);
		if (id == null
				|| update(uri, values, Contract._ID + "=" + id, null) < 1) {
			created = Contract.build(db.insert(Contract.TABLE, null, values));
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return created;
	}

	@Override
	public String getType(Uri uri) {
		switch (MATCHER.match(uri)) {
		case SPOT_ID:
			return Contract.CONTENT_ITEM_TYPE;
		case SPOTS:
			return Contract.CONTENT_TYPE;
		default:
			throw new SQLException("failed to match uri: " + uri);
		}
	}

	@Override
	public boolean onCreate() {
		this.helper = new SpotHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor c = null;
		SQLiteDatabase db = this.helper.getReadableDatabase();
		switch (MATCHER.match(uri)) {
		case SPOT_ID:
			c = db.query(Contract.TABLE, projection, Contract._ID + "="
					+ Contract.getId(uri), null, null, null, sortOrder);
			break;
		case SPOTS:
			c = db.query(Contract.TABLE, projection, selection, selectionArgs,
					null, null, sortOrder);
			break;
		}
		if (c != null) {
			c.setNotificationUri(getContext().getContentResolver(), uri);
		}
		return c;
	}

}
